﻿function explode(slot)
{
	var x;
	var y;
	switch (slot)
	{
		case 1:
			x = 2;
			y = 1;
			break;
		case 2:
			x = 3;
			y = 2;
			break;
		case 3:
			x = 4;
			y = 1;
			break;
		case 4:
			x = 4;
			y = 3;
			break;
		case 5:
			x = 3;
			y = 2;
			break;
	}
	var mX = x * 100;
	var mY = y * 100;
	$('.explode').css({left: mX, top: mY}).show();
}
function map()
{ //klasa mapy
	this.clear = function ()
	{ //czysci mape przed ponownym narysowaniem
		modelObject.canvasHandler.clearRect(0, 0, modelObject.mapWidth, modelObject.mapHeight);
		modelObject.canvasHandler.beginPath();
		return true;
	}
	this.drawObjects = function ()
	{ //rysuje wszystkie obiekty w zasiegu usera
		for (i in modelObject.mapTab)
		{ //petli wiersze w tablicy obiektow zeby narysowac obiekty na mapie
			for (j in modelObject.mapTab[i])
			{ //petli kolumny w wierszu tablicy obiektow
				var mapPoint = modelObject.mapTab[i][j];
				var absoluteX = j * modelObject.squareSide; //x absolutne
				var absoluteY = i * modelObject.squareSide; //y absolutne

				var text = null;
				var text2 = null;
				var slotNo = saveElement(absoluteX, absoluteY, "no", false)
				if(slotNo) {
					var type = engine.getElementType(slotNo);
					text = "Current:" + Math.round(engine.getValue("R" + slotNo).I*10000) /10000;
					text2  = "Voltage:" + Math.round(engine.getValue("R" + slotNo).V*10000) /10000;
				}

					viewObject.squareDraw(
						absoluteX,
						absoluteY,
						modelObject.squareSide,
						modelObject.mapTabImg[mapPoint],
						text,
						text2
				); //generuje tlo (trawnik)
			}
		}
		return true;
	}

	this.drawFirst = function ()
	{ //pierwsze narysowanie mapy
		if (modelObject.canvasHandler !== undefined)
		{ //jesli mapa jest
			this.clear();
		}
		//INICJUJE
		$('#game .canvasMask').css(
			'width', modelObject.mapWidth+'px'
		).css(
			'height', modelObject.mapHeight+'px'
		);
		$('#game .canvasMask #canvasWbmmo').attr(
			'width', modelObject.mapWidth+''
		).attr(
			'height', modelObject.mapHeight+''
		);
		var wbmmoCanvas = document.getElementById('canvas');
		modelObject.canvasHandler = wbmmoCanvas.getContext('2d');
		this.drawObjects();
		return true;
	}
}




function saveElement(x, y, type, draw)
{ //dostaje wspolrzedne X i Y i nazwe pliku graficznego





	var roundX = Math.round(x / 100, 2);
	var roundY = Math.round(y / 100, 2);


	if ( (roundX == 2) && (roundY == 1) )
	{ //slot 1
		if(draw)
		{

			modelObject.mapTab[roundY][roundX] = '00';
			modelObject.mapTab[roundY][roundX] = modelObject.mapTabImgRev[type];
		}
		return 1;
	}
	else if ( (roundX == 3) && (roundY == 2) )
	{ //slot 5
		if(draw)
{

			modelObject.mapTab[roundY][roundX] = '00';
			modelObject.mapTab[roundY][roundX] = modelObject.mapTabImgRev[type];
		}
		return 2;
	}
	else if ( (roundX == 4) && (roundY == 1) )
	{ //slot 3
		if(draw)
{
			modelObject.mapTab[roundY][roundX] = '00';
			modelObject.mapTab[roundY][roundX] = modelObject.mapTabImgRev[type];
		}
		return 3;
	}
	else if ( (roundX == 2) && (roundY == 3) )
	{ //slot 4
		if(draw)
{
			modelObject.mapTab[roundY][roundX] = '00';
			modelObject.mapTab[roundY][roundX] = modelObject.mapTabImgRev[type];
		}
		return 4;
	}

	else if ( (roundX == 4) && (roundY == 3) )
	{ //slot 4
		if(draw)
{
			modelObject.mapTab[roundY][roundX] = '00';
			modelObject.mapTab[roundY][roundX] = modelObject.mapTabImgRev[type];
		}
		return 5;
	}


	return false;
}



function view()
{ //widok czyli klasa zajmujaca sie grafika
	this.actionsClear = function ()
	{ //czysci liste akcji w menu
		$('.sidebar .actions').html('');
	}
	this.actionsSet = function (name, mode)
	{ //dodaje akcje do listy w menu
		if (mode == 1)
		{
			$('.sidebar .actions').append(
				'<span style="font-weight: bold; color: red;">[SPACE] fight with ' + name + '</span>'
			);
		}
	}
	this.squareDraw = function(x, y, edge, texture, text, text2)
	{ //rysuje oteksturowany kwadrat
		if(texture) {
			var myImage = new Image();
			myImage.src = 'img/' + texture + '.png';
			modelObject.canvasHandler.drawImage(
				myImage,
				x,
				y,
				edge,
				edge
			);
		}
		if (text)
		{ //jesli na obiekcie trzeba napisac jakis tekst
			modelObject.canvasHandler.fillStyle = '#F00';
			modelObject.canvasHandler.font = 'bold 18px arial';
			modelObject.canvasHandler.textBaseline = 'top';
			modelObject.canvasHandler.fillText(text, x, y);
			modelObject.canvasHandler.fillStyle = '#000';
			modelObject.canvasHandler.fillText(text2, x, y + 13);
		}
		return true;
	}
}

function model()
{ //model czyli klasa zajmujaca sie danymi
	this.mapWidth = 1000;
	this.mapHeight = 1000;
	this.canvasHandler; //uchwyt canvasa
	this.moveSpeed = 1000; //co ile rysowac, sprawdzac kolizje, wysylac dane
	this.squareSide = 100; //bok kwadratu
	this.mapTab = new Array(); //pierwszy wymiar tablicy map

	this.mapTab = [
		['00','00','00','00','00','00','00','00','00','00'],
		['00','13','00','15','00','11','00','00','00','00'],
		['00','07','00','00','00','07','00','00','00','00'],
		['00','14','00','18','00','12','00','00','00','00'],
		['00','00','00','00','00','00','00','00','00','00'],
		['00','00','00','00','00','00','00','00','00','00'],
		['00','00','00','00','00','00','00','00','00','00'],
		['00','00','00','00','00','00','00','00','00','00'],
		['00','00','00','00','00','00','00','00','00','00'],
		['00','00','00','00','00','00','00','00','00','00']
	]; //obiekty na mapie


this.mapTabImg = new Array(); //pierwszy wymiar tablicy map


this.mapTabImg['00'] = null;
this.mapTabImg['01'] = 'line';
this.mapTabImg['02'] = 'linev';
this.mapTabImg['03'] = 'lightbulb';
this.mapTabImg['04'] = 'lightbulbv';
this.mapTabImg['05'] = 'resistor';
this.mapTabImg['06'] = 'resistorv';
this.mapTabImg['07'] = 'source';
this.mapTabImg['08'] = 'sourcev';
this.mapTabImg['09'] = 'switch';
this.mapTabImg['10'] = 'switchv';
this.mapTabImg['11'] = 'cornerbottomleft';
this.mapTabImg['12'] = 'cornertopleft';
this.mapTabImg['13'] = 'cornerbottomright';
this.mapTabImg['14'] = 'cornertopright';
this.mapTabImg['15'] = 'threebottom';
this.mapTabImg['16'] = 'threeright';
this.mapTabImg['17'] = 'threeleft';
this.mapTabImg['18'] = 'threetop';
this.mapTabImg['19'] = 'cross';

this.mapTabImgRev = new Array(); //pierwszy wymiar tablicy map

/*
for(var element in this.mapTabImg)
{
	this.mapTabImgRev[this.mapTabImg]
}
*/

for (i in this.mapTabImg) {
  //document.writeln(i+':'+associativeArray[i]+', ');


this.mapTabImgRev[this.mapTabImg[i]] = i;


};

}


//CORE

var modelObject = new model();
var mapObject = new map();
var viewObject = new view();
var thingArray = new Array(); //tablica referencji obiektow klasy thing

$(document).ready(function()
{

formObj.hide();

$('#canvas').click(function() {
	//alert('klik');
	formObj.show();
});


	mapObject.drawFirst(); //umieszcza obiekty na mapie
	setInterval(
		function()
		{
			if (modelObject.canvasHandler !== undefined)
			{ //jesli mapa jest
				modelObject.canvasHandler.clearRect(0, 0, modelObject.mapWidth, modelObject.mapHeight); //to jest hack
				mapObject.drawObjects();
			}
		},
		modelObject.moveSpeed
	);
});
